﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CmsSite
    {
        public int Add(KeRui.Model.CmsSite model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@SiteName", SqlDbType.NVarChar, 50), new SqlParameter("@SiteAlias", SqlDbType.NVarChar, 50), new SqlParameter("@SiteKey", SqlDbType.VarChar, 50), new SqlParameter("@ServerID", SqlDbType.Int, 4), new SqlParameter("@SiteDir", SqlDbType.NVarChar, 500), new SqlParameter("@TemplateID", SqlDbType.Int), new SqlParameter("@IsUsing", SqlDbType.Int, 4), new SqlParameter("@OrderID", SqlDbType.Int, 4) };
            parameters[0].Value = model.SiteName;
            parameters[1].Value = model.SiteAlias;
            parameters[2].Value = model.SiteKey;
            parameters[3].Value = model.ServerID;
            parameters[4].Value = model.SiteDir;
            parameters[5].Value = model.TemplateID;
            parameters[6].Value = model.IsUsing;
            parameters[7].Value = model.OrderID;
            return DbHelperSQL.RunProcedure("CmsSite_ADD", parameters, out num);
        }

        public void Delete(int SiteID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@SiteID", SqlDbType.Int, 4) };
            parameters[0].Value = SiteID;
            DbHelperSQL.RunProcedure("CmsSite_Delete", parameters, out num);
        }

        public KeRui.Model.CmsSite GetModel(int SiteID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@SiteID", SqlDbType.Int, 4) };
            parameters[0].Value = SiteID;
            KeRui.Model.CmsSite site = new KeRui.Model.CmsSite();
            DataSet set = DbHelperSQL.RunProcedure("CmsSite_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["SiteID"].ToString() != "")
                {
                    site.SiteID = int.Parse(set.Tables[0].Rows[0]["SiteID"].ToString());
                }
                site.SiteName = set.Tables[0].Rows[0]["SiteName"].ToString();
                site.SiteAlias = set.Tables[0].Rows[0]["SiteAlias"].ToString();
                site.SiteKey = set.Tables[0].Rows[0]["SiteKey"].ToString();
                site.SiteDomain = set.Tables[0].Rows[0]["SiteDomain"].ToString();
                site.SiteUrl = set.Tables[0].Rows[0]["SiteUrl"].ToString();
                site.SiteDir = set.Tables[0].Rows[0]["SiteDir"].ToString();
                if (set.Tables[0].Rows[0]["TemplateID"].ToString() != "")
                {
                    site.TemplateID = int.Parse(set.Tables[0].Rows[0]["TemplateID"].ToString());
                }
                site.TemplateName = set.Tables[0].Rows[0]["TemplateName"].ToString();
                site.TemplateFolder = set.Tables[0].Rows[0]["TemplateFolder"].ToString();
                site.TemplateDir = set.Tables[0].Rows[0]["TemplateDir"].ToString();
                site.NodeDefaultTemplateFile = set.Tables[0].Rows[0]["NodeDefaultTemplateFile"].ToString();
                site.Domain = set.Tables[0].Rows[0]["Domain"].ToString();
                if (set.Tables[0].Rows[0]["ServerID"].ToString() != "")
                {
                    site.ServerID = int.Parse(set.Tables[0].Rows[0]["ServerID"].ToString());
                }
                site.ServerIP = set.Tables[0].Rows[0]["ServerIP"].ToString();
                if (set.Tables[0].Rows[0]["IsUsing"].ToString() != "")
                {
                    site.IsUsing = int.Parse(set.Tables[0].Rows[0]["IsUsing"].ToString());
                }
                if (set.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    site.OrderID = int.Parse(set.Tables[0].Rows[0]["OrderID"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    site.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return site;
            }
            return null;
        }

        public KeRui.Model.CmsSite GetModel(string SiteDomain)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@SiteDomain", SqlDbType.VarChar, 100) };
            parameters[0].Value = SiteDomain;
            KeRui.Model.CmsSite site = new KeRui.Model.CmsSite();
            DataSet set = DbHelperSQL.RunProcedure("CmsSite_GetModelbyDomain", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["SiteID"].ToString() != "")
                {
                    site.SiteID = int.Parse(set.Tables[0].Rows[0]["SiteID"].ToString());
                }
                site.SiteName = set.Tables[0].Rows[0]["SiteName"].ToString();
                site.SiteAlias = set.Tables[0].Rows[0]["SiteAlias"].ToString();
                site.SiteKey = set.Tables[0].Rows[0]["SiteKey"].ToString();
                site.SiteDomain = set.Tables[0].Rows[0]["SiteDomain"].ToString();
                site.SiteUrl = set.Tables[0].Rows[0]["SiteUrl"].ToString();
                site.SiteDir = set.Tables[0].Rows[0]["SiteDir"].ToString();
                if (set.Tables[0].Rows[0]["TemplateID"].ToString() != "")
                {
                    site.TemplateID = int.Parse(set.Tables[0].Rows[0]["TemplateID"].ToString());
                }
                site.TemplateName = set.Tables[0].Rows[0]["TemplateName"].ToString();
                site.TemplateFolder = set.Tables[0].Rows[0]["TemplateFolder"].ToString();
                site.TemplateDir = set.Tables[0].Rows[0]["TemplateDir"].ToString();
                site.NodeDefaultTemplateFile = set.Tables[0].Rows[0]["NodeDefaultTemplateFile"].ToString();
                site.Domain = set.Tables[0].Rows[0]["Domain"].ToString();
                if (set.Tables[0].Rows[0]["ServerID"].ToString() != "")
                {
                    site.ServerID = int.Parse(set.Tables[0].Rows[0]["ServerID"].ToString());
                }
                site.ServerIP = set.Tables[0].Rows[0]["ServerIP"].ToString();
                if (set.Tables[0].Rows[0]["IsUsing"].ToString() != "")
                {
                    site.IsUsing = int.Parse(set.Tables[0].Rows[0]["IsUsing"].ToString());
                }
                if (set.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    site.OrderID = int.Parse(set.Tables[0].Rows[0]["OrderID"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    site.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return site;
            }
            return null;
        }

        public KeRui.Model.CmsSite GetSiteModel(string SiteName)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@SiteName", SqlDbType.VarChar, 50) };
            parameters[0].Value = SiteName;
            KeRui.Model.CmsSite site = new KeRui.Model.CmsSite();
            DataSet set = DbHelperSQL.RunProcedure("CmsSite_GetModelbySiteName", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["SiteID"].ToString() != "")
                {
                    site.SiteID = int.Parse(set.Tables[0].Rows[0]["SiteID"].ToString());
                }
                site.SiteName = set.Tables[0].Rows[0]["SiteName"].ToString();
                site.SiteAlias = set.Tables[0].Rows[0]["SiteAlias"].ToString();
                site.SiteKey = set.Tables[0].Rows[0]["SiteKey"].ToString();
                site.SiteDomain = set.Tables[0].Rows[0]["SiteDomain"].ToString();
                site.SiteUrl = set.Tables[0].Rows[0]["SiteUrl"].ToString();
                site.SiteDir = set.Tables[0].Rows[0]["SiteDir"].ToString();
                if (set.Tables[0].Rows[0]["TemplateID"].ToString() != "")
                {
                    site.TemplateID = int.Parse(set.Tables[0].Rows[0]["TemplateID"].ToString());
                }
                site.TemplateName = set.Tables[0].Rows[0]["TemplateName"].ToString();
                site.TemplateFolder = set.Tables[0].Rows[0]["TemplateFolder"].ToString();
                site.TemplateDir = set.Tables[0].Rows[0]["TemplateDir"].ToString();
                site.NodeDefaultTemplateFile = set.Tables[0].Rows[0]["NodeDefaultTemplateFile"].ToString();
                site.Domain = set.Tables[0].Rows[0]["Domain"].ToString();
                if (set.Tables[0].Rows[0]["ServerID"].ToString() != "")
                {
                    site.ServerID = int.Parse(set.Tables[0].Rows[0]["ServerID"].ToString());
                }
                site.ServerIP = set.Tables[0].Rows[0]["ServerIP"].ToString();
                if (set.Tables[0].Rows[0]["IsUsing"].ToString() != "")
                {
                    site.IsUsing = int.Parse(set.Tables[0].Rows[0]["IsUsing"].ToString());
                }
                if (set.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    site.OrderID = int.Parse(set.Tables[0].Rows[0]["OrderID"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    site.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return site;
            }
            return null;
        }

        public int Update(KeRui.Model.CmsSite model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@SiteID", SqlDbType.Int, 4), new SqlParameter("@SiteName", SqlDbType.NVarChar, 50), new SqlParameter("@SiteAlias", SqlDbType.NVarChar, 50), new SqlParameter("@SiteKey", SqlDbType.VarChar, 50), new SqlParameter("@ServerID", SqlDbType.Int, 4), new SqlParameter("@IsUsing", SqlDbType.Int, 4), new SqlParameter("@OrderID", SqlDbType.Int, 4), new SqlParameter("@TemplateID", SqlDbType.Int) };
            parameters[0].Value = model.SiteID;
            parameters[1].Value = model.SiteName;
            parameters[2].Value = model.SiteAlias;
            parameters[3].Value = model.SiteKey;
            parameters[4].Value = model.ServerID;
            parameters[5].Value = model.IsUsing;
            parameters[6].Value = model.OrderID;
            parameters[7].Value = model.TemplateID;
            return DbHelperSQL.RunProcedure("CmsSite_Update", parameters, out num);
        }
    }
}

